$(function(){
//search function
$("#searchtxt").focus(function(){
	if($(this).val() == "search..."){
		$(this).val("");
	}
}).blur(function(){
	if($(this).val() == ""){
		$(this).val("search...");
	}
});
var all_search = $('input#searchtxt').quicksearch('table tbody tr:visible',
		{   
			show: function () {
				this.style.display = "";
			},
			hide: function () {
				this.style.display = "none";
			}
	    });
var ut_search = $('input#u_type').quicksearch('table tbody tr:visible',
		{
			selector: 'td.ut',
			
			prepareQuery: function (val) {
				return val.toLowerCase().split(' ');
			}
		});

var un_search = $('input#u_name').quicksearch('table tbody tr:visible',
		{
			selector: 'td.un',
			
			prepareQuery: function (val) {
				return val.toLowerCase().split(' ');
			}
		});

var uh_search = $('input#u_head').quicksearch('table tbody tr:visible',
		{
			selector: 'td.uh',
			
			prepareQuery: function (val) {
				return val.toLowerCase().split(' ');
			}
		});

var cdf_search = $('input#close_d_f').quicksearch('table tbody tr:visible',
		{
			selector: 'td.cd',
			bind: 'blur',
			onBefore: function(){
				if($('#close_d_f').val() == "" && $('#close_d_t').val() != ""){
					cdt_search();
					return;
				}
			},
			prepareQuery: function (val) {
				return val.toLowerCase().split(' ');
			},
			testQuery: function(query, txt, _row){
				var d1 = new Date(Date.parse(query));
				var d2 = new Date(Date.parse(txt));
				var dt_val = $('#close_d_t').val();
				var d3 = (dt_val == "") ? "" : new Date(Date.parse(dt_val));
				if(d3 != ""){
					return d1 <= d2 && d2 <= d3;
				}else{
					return d1 <= d2;
				}
			}
		});
var cdt_search = $('input#close_d_t').quicksearch('table tbody tr:visible',
		{
			selector: 'td.cd',
			bind: 'blur',
			onBefore: function(){
				if($('#close_d_t').val() == "" && $('#close_d_f').val() != ""){
					cdf_search();
					return;
				}
			},
			prepareQuery: function (val) {
				return val.toLowerCase().split(' ');
			},
			testQuery: function(query, txt, _row){
				var d3 = new Date(Date.parse(query));
				var d2 = new Date(Date.parse(txt));
				var df_val = $('#close_d_f').val();
				var d1 = (df_val == "") ? "" : new Date(Date.parse(df_val));
				if(d1 != ""){
					return d1 <= d2 && d2 <= d3;
				}else{
					return d2 <= d3;
				}
			}
		});

var ps_search = $('#period_s').quicksearch('table tbody tr:visible',
		{
			selector: 'td.cd',
			bind: 'change',
			prepareQuery: function (val) {
				var date = new Date();
				var m = date.getMonth();
				var y = date.getFullYear();
				if(val == 0){
					val = "";
				}else if(val < 12){
					m = m - val;
					if(m < 0){
						m = m + 12;
						y = y -1;
					}
				}else if(val >= 12){
					y = y - val/12;
				}
				date.setMonth(m);
				date.setFullYear(y);
				return val == "" ? val : date;
			},
			testQuery: function(query, txt, _row){
				var d = new Date(Date.parse(txt));
				return query <= d;
			}
		}
);
//filter
$('#f_draft a').click(function(){
	$('table tbody tr:not(:contains(Draft))').css("display","none");
	$('table tbody tr:contains(Draft)').css("display","");
	$('#f_all').removeClass('current');
	$('#f_active').removeClass('current');
	$('#f_draft').addClass('current');
});

$('#f_active a').click(function(){
	$('table tbody tr:not(:contains(Active))').css("display","none");
	$('table tbody tr:contains(Active)').css("display","");
	$('#f_all').removeClass('current');
	$('#f_draft').removeClass('current');
	$('#f_active').addClass('current');
});

$('#f_all a').click(function(){
	$('table tbody tr').css("display","");
	$('#f_draft').removeClass('current');
	$('#f_active').removeClass('current');
	$('#f_all').addClass('current');
});

//radio function
$('.radio_s').click(function(){
	var v = $(this).attr("value");
	if(v == 0){
		$('.default_e').removeAttr("disabled");
		$('.default_d').attr("disabled","disabled");
	}else if(v == 1){
		$('.default_d').removeAttr("disabled");
		$('.default_e').attr("disabled","disabled");
	}
});

//datepicker
var dates = $( "#close_d_f, #close_d_t" ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 3,
			onSelect: function( selectedDate ) {
				var option = this.id == "close_d_f" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
				$(this).blur();
			}
});

//tree
$(".tree").treeview({animated: "medium",collapsed: true});
//end js
});
